package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.HealthManage;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return memberService.pageQuery(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.upDate(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            memberService.delete(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    @RequestMapping("/exportMemberProfile")
    public Result exportMemberProfile(@RequestParam Integer[] memberIdData, HttpServletRequest request, HttpServletResponse response) {
        try {
            //取出返回结果数据，准备将报表数据写入到Excel文件中
            List<Member> list = memberService.findMemberIds(memberIdData);
            //获取保存在template中额Excel的模板
            String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "member.xlsx";
            //基于提供的Excel模板文件在内存中创建一个Excel表格对象
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //读取工作表格的中第一个表格
            XSSFSheet sheet = excel.getSheetAt(0);
            int rowNum = 1;
            for (Member member : list) {
                Integer id = member.getId();
                String fileNumber = member.getFileNumber();
                String name = member.getName();
                String sex = member.getSex();
                String idCard = member.getIdCard();
                String phoneNumber = member.getPhoneNumber();
                Date regTime = member.getRegTime();
                String password = member.getPassword();
                String email = member.getEmail();
                String birthday = DateUtils.parseDate2String(member.getBirthday());
                String remark = member.getRemark();
                //获取对应的行的数据
                XSSFRow row = sheet.getRow(rowNum++);
                row.getCell(0).setCellValue(id);
                row.getCell(1).setCellValue(fileNumber);
                row.getCell(2).setCellValue(name);
                row.getCell(3).setCellValue(sex);
                row.getCell(4).setCellValue(idCard);
                row.getCell(5).setCellValue(phoneNumber);
                row.getCell(6).setCellValue(regTime);
                row.getCell(7).setCellValue(password);
                row.getCell(8).setCellValue(email);
                row.getCell(9).setCellValue(birthday);
                row.getCell(10).setCellValue(remark);
            }
            //使用输出流进行表格下载,基于浏览器作为客户端下载,提示对应的输出的语句
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("content-Disposition", "attachment;filename=reportMember.xlsx");//指定以附件形式进行下载
            excel.write(out);

            out.flush();
            out.close();
            excel.close();

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
